package com.mybatis.panel;

import com.mybatis.entity.Student;
import com.mybatis.service.StudentService;
import com.mybatis.util.DataUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class StudentInsertPanel extends JPanel {

    public StudentInsertPanel() {
        setBounds(0, 0, 900, 600);

        JPanel panel2 = new JPanel();
        add(panel2, BorderLayout.NORTH);

        JButton button = new JButton("返回主页");
        add(button);

        JButton insertButton = new JButton("向表插入行中数据");
        add(insertButton);


        //下拉框
        List[] box = StudentService.getComboBox();

        JComboBox<String> jComboBox1 = new JComboBox<>();
        jComboBox1.addItem("班级编号");
        JComboBox<String> jComboBox2 = new JComboBox<>();
        jComboBox2.addItem("专业编号");
        JComboBox<String> jComboBox3 = new JComboBox<>();
        jComboBox3.addItem("学院编号");
        for (Object o : box[0])
            jComboBox1.addItem(o.toString());
        for (Object o : box[1])
            jComboBox2.addItem(o.toString());
        for (Object o : box[2])
            jComboBox3.addItem(o.toString());

        add(jComboBox1);
        add(jComboBox2);
        add(jComboBox3);



        //表格
        JTable table = new JTable() {
            @Override
            public boolean isCellEditable(int row, int column) {
                //可以直接编辑表格
                return true;
            }
        };

        Object[] columnNames = {"学号", "姓名", "性别", "民族", "年龄", "出生日期", "班级编号", "专业编号", "学院编号"};

        List<Student> list = new ArrayList<>();
        list.add(new Student());
        //List<Student> list = StudentService.getPage((pageSize - 1) * 30);

        Object[][] objects = DataUtil.getConvertData(list);

        DefaultTableModel tableModel = new DefaultTableModel(objects, columnNames);

        //设置表格数据只能单选
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //设置表格数据
        table.setModel(tableModel);

        //panel.add(table);
        //panel.add(table.getTableHeader(), BorderLayout.NORTH);
        add(table.getTableHeader(), BorderLayout.NORTH);
        //把表格内容添加到容器中心
        //panel.add(table, BorderLayout.CENTER);
        add(table, BorderLayout.CENTER);



        //================按钮事件===================

        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                MainJFrame.setPanel(new StudentPanel());
            }
        });

        insertButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Student student = new Student();
                Object[] objects = new Object[9];
                for (int i = 0; i < 9; i++) {
                    objects[i] = table.getValueAt(0, i);
                    //System.out.println("======================================");
                    //System.out.println(table.getValueAt(0, i).getClass());
                }
                student.setSno((String) objects[0]);
                student.setSname((String) objects[1]);
                student.setSex((String) objects[2]);
                student.setNation((String) objects[3]);
                student.setAge(Integer.parseInt(objects[4].toString()));
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                student.setBirthday(LocalDate.parse(objects[5].toString(), formatter));
                student.setCid((String) objects[6]);
                student.setMno((String) objects[7]);
                student.setDno((String) objects[8]);

                if (StudentService.getOne(student)) {
                    JOptionPane.showMessageDialog(panel2,
                            "插入失败,数据已存在!", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                StudentService.insert(student);
                JOptionPane.showMessageDialog(panel2,
                        "插入成功!", "系统提示", JOptionPane.WARNING_MESSAGE);

            }
        });
    }
}
